{*
  Object Pascal simple float number manipulation routines
  @Author  Maciej Zagozda (1998-2008)
  @Licence Microsoft Reciprocal License (Ms-RL)
  @Website http://www.codeplex.com/intelitechdelphirtns/
  @Version 2008/04/20 Added documentation support
}


{$IFDEF VER110}
  {$DEFINE BorlandDelphi3}
{$ENDIF}

{$IFDEF VER120}
  {$DEFINE BorlandDelphi4}
{$ENDIF}

{$IFDEF VER130}
  {$DEFINE BorlandDelphi3}
  {$DEFINE BorlandDelphi4}
  {$DEFINE BorlandDelphi5}
{$ENDIF}

{$IFDEF VER140}
  {$DEFINE BorlandDelphi3}
  {$DEFINE BorlandDelphi4}
  {$DEFINE BorlandDelphi5}
  {$DEFINE BorlandDelphi6}
{$ENDIF}

{$IFDEF VER150}
  {$DEFINE BorlandDelphi3}
  {$DEFINE BorlandDelphi4}
  {$DEFINE BorlandDelphi5}
  {$DEFINE BorlandDelphi6}
  {$DEFINE BorlandDelphi7}
{$ENDIF}

{$IFDEF VER160}
  {$DEFINE BorlandDelphi3}
  {$DEFINE BorlandDelphi4}
  {$DEFINE BorlandDelphi5}
  {$DEFINE BorlandDelphi6}
  {$DEFINE BorlandDelphi7}
  {$DEFINE BorlandDelphi8}
{$ENDIF}

{$IFDEF VER170}
  {$DEFINE BorlandDelphi3}
  {$DEFINE BorlandDelphi4}
  {$DEFINE BorlandDelphi5}
  {$DEFINE BorlandDelphi6}
  {$DEFINE BorlandDelphi7}
  {$DEFINE BorlandDelphi8}
  {$DEFINE BorlandDelphi2005}
{$ENDIF}

{$IFDEF VER180}
  {$DEFINE BorlandDelphi3}
  {$DEFINE BorlandDelphi4}
  {$DEFINE BorlandDelphi5}
  {$DEFINE BorlandDelphi6}
  {$DEFINE BorlandDelphi7}
  {$DEFINE BorlandDelphi8}
  {$DEFINE BorlandDelphi2005}
  {$DEFINE BorlandDelphi2006}
{$ENDIF}

{$IFDEF VER190}
  {$DEFINE BorlandDelphi3}
  {$DEFINE BorlandDelphi4}
  {$DEFINE BorlandDelphi5}
  {$DEFINE BorlandDelphi6}
  {$DEFINE BorlandDelphi7}
  {$DEFINE BorlandDelphi8}
  {$DEFINE BorlandDelphi2005}
  {$DEFINE BorlandDelphi2006}
  {$DEFINE BorlandDelphi2007}
{$ENDIF}

unit FloatRoutines;

interface

uses
  Classes,
  Dialogs,
  Math,
  Messages,
  SysUtils,
  Windows;

function CurrencyToDouble( currValue: Currency ): Double;
function DoblValue( strnString: AnsiString ): Double;
function FloatToString( doblFloat: Double ): AnsiString;
// round a number up to specified number of digits.
{$IFDEF BorlandDelphi7} function RoundValue(const AValue: Double; const ADigit: TRoundToRange = -2): Double; {$ENDIF}
function SingValue( strnString: AnsiString ): Single;
//function Power( X, Y: Extended ): Extended;
function AsNegative(const doblValue: Double): Double;

implementation

function CurrencyToDouble( currValue: Currency ): Double;
var
  doblValue: Double;
begin
  try
    doblValue := currValue;
  except
    doblValue := 0;
  end;//try
  Result := doblValue;
end;

//_____________________________________________________________________________

function DoblValue( strnString: AnsiString ): Double;
var
  boolPositive, boolE: Boolean;
  doblResult: Double;
begin

  Result := 0;
  boolPositive := True;
  boolE := False;

  // process thousand separators

  strnString := StringReplace(strnString, '''', '', [rfReplaceAll]);
  strnString := StringReplace(strnString, ' ', '', [rfReplaceAll]);

  // remove value signs

  if Pos( '+', strnString ) = 1 then begin strnString := Copy( strnString, 2, 255 ); boolPositive := True; end;
  if Pos( '-', strnString ) = 1 then begin strnString := Copy( strnString, 2, 255 ); boolPositive := False; end;

  // remove leading dot or comma

  if Pos( '.', strnString ) = 1 then strnString := Copy( strnString, 2, 255 );
  if Pos( '.', strnString ) = Length( strnString ) then strnString := Copy( strnString, 1, Length( strnString ) -1 );
  if Pos( ',', strnString ) = 1 then strnString := Copy( strnString, 2, 255 );
  if Pos( ',', strnString ) = Length( strnString ) then strnString := Copy( strnString, 1, Length( strnString ) -1 );

  // convert decimal separator

  // if the current decimal separator is not a dot then replace all dots with the current decimal separator symbol
  if AnsiCompareText( DecimalSeparator, '.' ) <> 0 then strnString := StringReplace( strnString, '.', DecimalSeparator, [rfReplaceAll] );

  // if the current decimal sepatator is not a comma then replace all commas with the current decimal separator symbol
  if AnsiCompareText( DecimalSeparator, ',' ) <> 0 then strnString := StringReplace( strnString, ',', DecimalSeparator, [rfReplaceAll] );

  // Convert output string using StrToFloat procedure
  //ShowMessage( 'DoblValue output string: ' + strnString );

  try
    doblResult := StrToFloat( strnString );
  except
    doblResult := 0;
    boolE := True;
  end;//try

  // convert the value

  if boolE = False then
  begin
    if boolPositive then
    begin
      Result := ABS( doblResult );
    end
    else
    begin
      if doblResult > 0 then
      begin
        Result := doblResult * -1;
      end
      else
      begin
        Result := doblResult;
      end;//if
    end;//if
  end
  else
  begin
    //ShowMessage( 'E@:'+IntToStr( intgErrorCode ));
    Result := 0;
  end;//if
end;

//_____________________________________________________________________________

function FloatToString( doblFloat: Double ): AnsiString;
begin
  Result := Format( '%.2f', [doblFloat] );
end;

//_____________________________________________________________________________

{$IFDEF BorlandDelphi7}
function RoundValue(const AValue: Double; const ADigit: TRoundToRange = -2): Double;
var
  LFactor: Double;
begin
  LFactor := IntPower(10, ADigit);
  Result := Trunc((AValue / LFactor) + 0.5) * LFactor;
end;
{$ENDIF}

//_____________________________________________________________________________

function SingValue( strnString: AnsiString ): Single;
var
  intgErrorCode: Integer;
begin
  Val( strnString, Result, intgErrorCode );
end;

//_____________________________________________________________________________

{
function Power( X, Y: Extended): Extended;
begin
  Result := Exp( Y * Ln( X ));
end;
}

//_____________________________________________________________________________

function AsNegative(const doblValue: Double): Double;
begin
  Result := ABS(doblValue)*-1;
end;

end.
